package 队列.easy;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 用队列实现栈
 * @author Lw中
 * @date 2020/10/24 11:27
 */
public class ErErWu {

    Queue<Integer> queue1;
    Queue<Integer> queue2;

    /** Initialize your data structure here. */
    public ErErWu() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        // 讲需要插入栈的元素放入队列2
        queue2.offer(x);
        // 将队列1中的poll出，然后offer进入队列2，放在x元素的后面
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        // 将queue1和queue2进行交换
        Queue<Integer> temp = new LinkedList<>();
        queue1 = queue2;
        queue2 = temp;
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue1.poll();
    }

    /** Get the top element. */
    public int top() {
        return queue1.peek();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue1.isEmpty();
    }

}
